package com.atguigu.gmall.ums.controller;

import java.util.List;

import com.sun.org.apache.bcel.internal.generic.RETURN;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.atguigu.gmall.ums.entity.UserEntity;
import com.atguigu.gmall.ums.service.UserService;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.common.bean.PageParamVo;

/**
 * 用户表
 *
 * @author gangge
 * @email gangge@atguigu.com
 * @date 2022-09-04 10:23:19
 */
@Api(tags = "用户表 管理")
@RestController
@RequestMapping("ums/user")
public class UserController {

    @Autowired
    private UserService userService;
    //GET /ums/user/query
    @ApiOperation(("查询用户数据"))
    @GetMapping("query")
    public ResponseVo query(@RequestParam(value = "loginName",required = true) String loginName,
                            @RequestParam(value = "password",required = true) String password){
        UserEntity userEntity = userService.queryUser(loginName,password);
        if(userEntity==null){
            return ResponseVo.fail("账号或密码错误");
        }
        return ResponseVo.ok(userEntity);
    }
    //POST /ums/user/register
    @ApiOperation("保存注册用户数据")
    @PostMapping("register")
    public ResponseVo register(UserEntity userEntity,
                               String code){
        userService.register(userEntity,code);
        return ResponseVo.ok();
    }
    //GET /ums/user/check/{data}/{type}
    @ApiOperation("数据唯一性校验")
    @GetMapping("check/{data}/{type}")
    public ResponseVo<Boolean> check(@PathVariable("data")String data,
                            @PathVariable("type")Integer type){
        Boolean b = userService.check(data,type);
        return ResponseVo.ok(b);
    }
    /**
     * 列表
     */
    @GetMapping
    @ApiOperation("分页查询")
    public ResponseVo<PageResultVo> queryUserByPage(PageParamVo paramVo){
        PageResultVo pageResultVo = userService.queryPage(paramVo);

        return ResponseVo.ok(pageResultVo);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("详情查询")
    public ResponseVo<UserEntity> queryUserById(@PathVariable("id") Long id){
		UserEntity user = userService.getById(id);

        return ResponseVo.ok(user);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation("保存")
    public ResponseVo<Object> save(@RequestBody UserEntity user){
		userService.save(user);

        return ResponseVo.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    public ResponseVo update(@RequestBody UserEntity user){
		userService.updateById(user);

        return ResponseVo.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseVo delete(@RequestBody List<Long> ids){
		userService.removeByIds(ids);

        return ResponseVo.ok();
    }

}
